package com.scy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scy.common.QueryPageBean;
import com.scy.entity.Medical;
import com.scy.service.MedicalService;
import com.scy.util.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medical")
public class MedicalController {
    @Autowired
    private MedicalService medicalService;

    @RequestMapping("/getMedicalPage")
    public String getMedicalPage(String queryPageBean){
        QueryPageBean queryPageBean1 = JSONObject.parseObject(queryPageBean, QueryPageBean.class);
        IPage<Medical> medicalPage = medicalService.getMedicalPage(queryPageBean1);
        Map<String,Object> map = new HashMap<>();
        map.put("list",medicalPage.getRecords());
        map.put("total",medicalPage.getTotal());
        String s = JSON.toJSONString(map);
        return s;
    }

    @RequestMapping("/addMedical")
    public String addMedical(String medical){
        Medical medical1 = JSONObject.parseObject(medical, Medical.class);
        medical1.setLogicDel(1);
        boolean save = medicalService.save(medical1);
        return ReturnUtil.returnString(save);
    }

    @RequestMapping("/getById")
    public String getById(String id){
        Medical byId = medicalService.getById(id);
        String s = JSON.toJSONString(byId);
        return s;
    }

    @RequestMapping("/updateMedical")
    public String updateMedical(String medical){
        Medical medical1 = JSONObject.parseObject(medical, Medical.class);
        boolean b = medicalService.updateById(medical1);
        return ReturnUtil.returnString(b);
    }

    @RequestMapping("/delMedical")
    public String delMedical(String medical){
        Medical medical1 = JSONObject.parseObject(medical, Medical.class);
        medical1.setLogicDel(0);
        boolean b = medicalService.updateById(medical1);
        return ReturnUtil.returnString(b);
    }

    //根据id集合查询药品id
    @RequestMapping("/getMedicalByIdList")
    public String getMedicalByIdList(String ids){
        if(!"".equals(ids)){
            String[] split = ids.split(",");
            QueryWrapper<Medical> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("medical_id",split);
            List<Medical> list = medicalService.list(queryWrapper);
            String s = JSON.toJSONString(list);
            return s;
        }else{
            return "";
        }
    }
}
